program ResidueProbability
implicit none

character(len=79)ARGUMENT
integer,parameter :: first=113
integer i,j,k,ios,connum
integer,allocatable :: c1(:),c2(:)
real,allocatable ::  p(:)
real res(first)

ios=0
i=0
open(11,file="nonnative.dat",status="old")
do while(.true.)
 read(11,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
 i=i+1
enddo
allocate(c1(i))
allocate(c2(i))
allocate(p(i))
 connum=i
 write(*,*) connum
rewind(11)
ios=0
i=0
do while(.true.)
 read(11,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
 i=i+1
 read(ARGUMENT(25:27),"(I3)") c1(i)
 read(ARGUMENT(32:34),"(I3)") c2(i)
 read(ARGUMENT(16:20),"(F5.3)") p(i)
enddo
close(11)

open(22,file="residueCutoff-6-8.5.dat",status="replace")
do j=1,first
     k=0
  do i=1,connum
   if(j==c1(i) .or. j==c2(i))then
      k=k+1
     res(j)=res(j)+p(i)
   endif
  enddo
     if(res(j)/=0)then
    write(22,"(I4,3X,I4,3X,F5.3,2X,F5.3)") j,k,res(j)/k,res(j)
     endif
enddo
close(22)
end program
